package com.example.administrator.waitan.find.life.LifeContentActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.tools.GsonRequest;
import com.example.administrator.waitan.tools.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class LifeContentActivity extends BaseActivity {
    private LifeContentViewPagerAdapter mAdapter;
    private ViewPager mViewPager;
    private List<LifeContentInfo> lifeContentInfos;
    private RequestQueue queue;
    private String url = "http://im.izhuti.cn/api/api_list.php?do=type&pid=type&tid=62&page=1&word=1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_content);
        mViewPager = (ViewPager) findViewById(R.id.vp_lifeContent);
        findViewById(R.id.btn_lifeContent_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Intent intent = getIntent();
        final Integer imgPosition = getIntent().getIntExtra("position", 2);
        GsonRequest<LifeContentBeans> lifeContentBeansGsonRequest = new GsonRequest<LifeContentBeans>(url, LifeContentBeans.class, new Response.Listener<LifeContentBeans>() {
            @Override
            public void onResponse(LifeContentBeans response) {
                Log.d("position",String.valueOf(imgPosition));

                mAdapter = new LifeContentViewPagerAdapter(LifeContentActivity.this,response.getList());
                mViewPager.setAdapter(mAdapter);
                Intent intent = new Intent(LifeContentActivity.this, DownLoadContentService.class);
                LifeContentActivity.this.bindService(intent, mAdapter.connection, Context.BIND_AUTO_CREATE);
                Toast.makeText(LifeContentActivity.this, "服务已经启动", Toast.LENGTH_SHORT).show();
                LifeContentActivity.this.startService(intent);
                mViewPager.setCurrentItem(imgPosition);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        VolleySingleton.getInstance()._addRequest(lifeContentBeansGsonRequest);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mAdapter.connection);
        Toast.makeText(LifeContentActivity.this, "服务已经取消", Toast.LENGTH_SHORT).show();
    }
}
